package com.csvc.controller.dashboard.cmdb;

import java.util.Objects;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csvc.business.cmdb.service.IOraSegmentService;
import com.csvc.commons.response.ResponseResult;
import com.csvc.repository.cmdb.domain.OraSegment;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author vincent zou
* @since 2020-12-29
*/
    @RestController
@RequestMapping("//ora-segment")
        public class OraSegmentController {
        @Resource
        private IOraSegmentService oraSegmentService;

        /**
        * 新增
        *
        * @param oraSegment {@link OraSegment}
        * @return {@link ResponseResult}
        */
        @PostMapping("create")
        public ResponseResult create(@Valid @RequestBody OraSegment oraSegment, BindingResult bindingResult) {
        // 表单验证
        if (bindingResult.hasErrors()) {
        return ResponseResult.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        // 业务逻辑
        boolean created = oraSegmentService.create(oraSegment);
        if (created) {
        return ResponseResult.success("创建成功");
        }

        return null;
        }

        /**
        * 删除
        *
        * @param oraSegmentId {@code Long}
        * @return {@link ResponseResult}
        */
        @DeleteMapping("remove/{oraSegmentId}")
        public ResponseResult remove(@PathVariable Long oraSegmentId) {
        // 业务逻辑
        boolean deleted = oraSegmentService.remove(oraSegmentId);
        if (deleted) {
        return ResponseResult.success("删除成功");
        }

        return null;
        }

        /**
        * 修改
        *
        * @param oraSegment {@link OraSegment}
        * @return {@link ResponseResult}
        */
        @PutMapping("update")
        public ResponseResult update(@Valid @RequestBody OraSegment oraSegment, BindingResult bindingResult) {
        // 表单验证
        if (bindingResult.hasErrors()) {
        return ResponseResult.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        // 业务逻辑
        boolean updated = oraSegmentService.update(oraSegment);
        if (updated) {
        return ResponseResult.success("编辑成功");
        }

        return null;
        }

        /**
        * 获取
        *
        * @param oraSegmentId {@code Long}
        * @return {@link ResponseResult}
        */
        @GetMapping("get/{oraSegmentId}")
        public ResponseResult get(@PathVariable Long oraSegmentId) {
        OraSegment oraSegment = oraSegmentService.get(oraSegmentId);
        return ResponseResult.success(oraSegment);
        }

        /**
        * 分页
        *
        * @param current {@code int} 页码
        * @param size    {@code int} 笔数
        * @return {@link ResponseResult}
        */
        @GetMapping("page")
        public ResponseResult page(
        @RequestParam int current, @RequestParam int size, @ModelAttribute OraSegment oraSegment) {
        IPage<OraSegment> page = oraSegmentService.page(current, size, oraSegment);
        return ResponseResult.success(page);
        }

    }
